<?php
/**
 * エラーコントローラー
 *
 * @author tanaka
 * @package Controller
 * @subpackage Error
 */
class ErrorController extends Controller_Action
{
	/**
	 * エラーアクション
	 *
	 * @return void
	 */
	public function errorAction()
	{
		$errors = $this->_getParam('error_handler');
		$exception = isset($errors->exception) && $errors->exception instanceof Exception ? $errors->exception : new UserException('', 404);

		$requestController = $this->getRequest()->getParam('controller');
		$requestAction = $this->getRequest()->getParam('action');

		$isChild = App::getInstance()->isChildWindow($requestController, $requestAction);

		if (_DEBUG) {
			$front = Zend_Controller_Front::getInstance();
			$front->registerPlugin(new Controller_Plugin_Debug_Exception($exception));
		}

		// デフォルト値の設定
		$code = 500;
		$message = '';

		// エラーコードの切り分け
		if ($exception instanceof AppException) {
			$code = $exception->getCode();
			$message = $exception->getShowMessage();
		}
		else {
			switch ($errors->type) {
				case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
				case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
				case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
					$code = 404;
					break;
			}
		}

		// タイトル、メッセージ設定
		switch ($code) {
			case 400:
				$title = '400 Bad Request';

				if(!strlen($message))
					$message = '変なリクエストが来たよ。';
				break;

			case 403:
				$title = '403 Forbidden';

				if(!strlen($message))
					$message = '権限がないよ。';
				break;

			case 404:
				$title = '404 Not Found';

				if(!strlen($message))
					$message = 'そんなページないよ。';
				break;

			default:
				$title = '500 Internal Server Error';
				File_Logger::getInstance(File_Logger::TYPE_APP_EXCEPTION)->write("{$exception->getMessage()}\n{$exception->getTraceAsString()}");

				if(!strlen($message))
					$message = 'なんかエラーが出たよ。';
				break;
		}

		$this->_setViewLayoutFile('nomenu');
		$this->_setViewRenderFile($isChild ? 'error_child' : 'error');

		// 表示
		$this->view->title = $title;
		$this->view->message = $message;
		$this->_response->setHttpResponseCode($code);
	}
	
	/**
	 * メッセージ表示を阻害
	 * @see Controller_Action::postDispatch()
	 */
	public function postDispatch()
	{}
}
